package com.zzj.ecology.system.utils;

import com.zzj.ecology.system.bean.system.SysMenu;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AppUtils
 * @Description TODO
 * @Author zhijun zeng at 22:55 2020/3/30
 * @Version 1.0
 **/
public class AppUtils {

    public static String createPageButtonPermsString(List<SysMenu> list) {
        StringBuilder builder = new StringBuilder();
        int i = 0;
        for (SysMenu mb : list) {
            //builder.append(StringUtils.substringBetween(mb.getPermissionCode(), "[","]"));
            String permCode = mb.getPermissionCode();
            String str1 = StringUtils.replace(permCode,"[","-");
            String str2 = StringUtils.replace(str1, "]", "");
            builder.append(str2);
            if (i < list.size() - 1) {
                builder.append("|");
            }
            i++;
        }
        return builder.toString();
    }

    public static List<String> createPageButtonPermsList(List<SysMenu> list) {
        List<String> list0 = new ArrayList<>();
        for (SysMenu mb : list) {
            String permCode = mb.getPermissionCode();
            String[] permArrs = permCode.split(":");
            //"sys|bus":"模块|user|role|url|perms":"create|update|select|delete|print|pdf"
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(permArrs[1]).append("-").append(permArrs[2]);
            list0.add(stringBuffer.toString());
        }
        return list0;
    }

    public static String createMenuIds2String(List<SysMenu> list) {
        StringBuilder builder = new StringBuilder();
        if (list.size() > 0) {
            for (SysMenu m : list) {
                builder.append(m.getMenuId()).append(",");
            }
            return builder.substring(0, builder.length()-1);
        }
        return "";
    }
}
